package eerbaar.v2.client.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.font.TextAttribute;
import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import eerbaar.v2.app.core.GamePanel;
import eerbaar.v2.app.core.IGamePanelListener;

public class GameStatusPanel extends GamePanel {

    private final JLabel label;
    private final JLabel labelValue;
    private int height = 16;
    private boolean showExtra;
    private final JTextArea textArea;

    public GameStatusPanel(IGamePanelListener listener, String text, int value) {
        super(listener);
        this.label = new JLabel();
        this.labelValue = new JLabel();
        this.textArea = new JTextArea();

        this.label.setText("" + text);
        this.setValue(value);

        addComponents();
        initComponents();
    }

    private void addComponents() {

        add(this.label);
        add(this.labelValue);
        add(this.textArea);

        this.textArea.setColumns(20);
        this.textArea.setRows(5);
        this.textArea.setBackground(javax.swing.UIManager.getDefaults()
                .getColor("Label.background"));

        this.label.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                labelMouseClicked(evt);
            }
        });
    }

    private int countLines() {
        int result = 1;
        String s = this.textArea.getText();
        s = s.trim();
        char[] chars = s.toCharArray();
        for (char c : chars) {
            if (c == '\n') {
                result++;
            }
        }
        return result;
    }

    private void initComponents() {

        if (this.showExtra) {
            int lines = this.countLines();
            int lineHeight = lines * 18;

            this.height = lineHeight + 20;
            this.textArea.setBounds(0, 18, 200, lineHeight - 2);
            this.textArea.setVisible(true);
        } else {
            this.height = 16;
            this.textArea.setVisible(false);
        }

        setPreferredSize(new Dimension(200, this.height));

        this.label.setBounds(0, 0, 50, 16 - 2);
        this.labelValue.setBounds(50, 0, 40, 16 - 2);
        this.labelValue.setHorizontalAlignment(SwingConstants.TRAILING);
    }

    private void labelMouseClicked(java.awt.event.MouseEvent evt) {
        this.showExtra = !this.showExtra;
        this.initComponents();
        super.getListener().onHeightChanged();
    }

    public void setExtra(String extra) {
        if (extra != null) {
            this.textArea.setText("" + extra);
            setupLink();
        }
        initComponents();
        super.getListener().onHeightChanged();
    }

    private void setupLink() {

        Font font = this.label.getFont();
        Map<TextAttribute, Object> map = new HashMap<TextAttribute, Object>();
        map.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
        font = font.deriveFont(map);
        this.label.setFont(font);

        this.label.setForeground(Color.BLUE);
        this.label.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
    }

    public void setValue(int value) {
        if (value >= 0) {
            NumberFormat format = NumberFormat.getIntegerInstance();
            String s = format.format(value);
            this.labelValue.setText(s);
        }
    }

}